﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace ggdll
{
    internal class Networking
    {
        private TcpClient client;
        private  NetworkStream clientStream;

        public delegate void ReceivedDataHandler(byte[] data);
        public event ReceivedDataHandler ReceivedData;

        public delegate void ReceiverStateHandler();
        public event ReceiverStateHandler ConnectionClosed;

        private Thread receiverThread;


        byte[] header;
        byte[] answer;
        int receivedDataCount = 0;
        int message_size;
        int answer_pos = 0;

        internal Networking(IPEndPoint IP)
        {
            client = new TcpClient();
            client.Connect(IP);
            clientStream = client.GetStream();
            
            receiverThread = new Thread(receiverThread_proc);
            receiverThread.Start();

        }

        internal int send(byte[] data)
        {
            try
            {
                clientStream.Write(data, 0, data.Length);
                return 1;
            }
            catch { return 0; }
        }

        internal void receiverThread_proc()
        {
            
            clientStream.ReadTimeout = -1;
            while (true)
            {
                try
                {
                    header = new byte[8];
                    receivedDataCount = clientStream.Read(header, 0, 8);
                    if (receivedDataCount != 0)
                    {
                        message_size = BitConverter.ToInt32(header, 4);
                        answer = new byte[message_size + 8];
                        System.Buffer.BlockCopy(header, 0, answer, 0, 8);
                        answer_pos=0;
                        do
                        {
                            answer_pos += clientStream.Read(answer, answer_pos + 8, message_size);
                            message_size -= answer_pos;
                        }
                        while (message_size > 0);
                        ReceivedData(answer);
                    }
                    else
                    {
                        ConnectionClosed();
                        break;
                    }
                    /*
                    receivedDataCount = clientStream.Read(buffer, 0, buffer.Length);
                    if (receivedDataCount != 0)
                    {
                        int size = BitConverter.ToInt32(buffer, 4);
                        byte[] answer = new byte[size + 8];
                        System.Buffer.BlockCopy(buffer, 0, answer, 0, receivedDataCount);
                        if (size > receivedDataCount - 8)
                        {
                            do
                            {
                                buffer = new byte[1024];
                                int receivedDataCount2 = clientStream.Read(buffer, 0, buffer.Length);
                                System.Buffer.BlockCopy(buffer, 0, answer, receivedDataCount, receivedDataCount2);
                                receivedDataCount += receivedDataCount2;
                            }
                            while (size < receivedDataCount - 8);
                        }

                        ReceivedData(answer);
                    }
                    else
                    {
                        ConnectionClosed();
                        break;
                    }*/
                }
                catch {
                    Thread.Sleep(0);
                }
                
            }
        }
        internal void Dispose()
        {
            client.Close();
            receiverThread.Abort();
            
        }


    }
}
